package com.bawei.module_home;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.mvp.v.BaseFragment;
import com.bawei.module_home.adapter.DiscountAdapter;
import com.bawei.module_home.adapter.VpBannerAdapter;
import com.bawei.module_home.commom.MainConstant;
import com.bawei.module_home.live.LiveActivity;
import com.bawei.module_home.mvp.GoodAdapter;
import com.bawei.module_home.mvp.Goods;
import com.bawei.module_home.mvp.GoodsContract;
import com.bawei.module_home.mvp.GoodsModel;
import com.bawei.module_home.mvp.GoodsPresenter;
import com.bawei.module_home.mvp.ScrollLinearLayoutManager;
import com.bawei.module_home.widget.DrawableCenterTextView;
import com.superluo.textbannerlibrary.TextBannerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.constants.PageStyle;

import java.util.ArrayList;
import java.util.List;


@Route(path = RouterPath.Home.PAGER_HOME)
public class HomeFragment extends BaseFragment<GoodsPresenter> implements GoodsContract.GoodsView {


    private Banner fragHomeBanner;
    private EditText fragHomeEdit;
    private TextBannerView fragHomeTextbanner;
    private RecyclerView fragHomeRv;
    private BannerViewPager layoutVpBanner;
    private RecyclerView mHomeGoodsList;
    private DrawableCenterTextView tvGoodstypeLive;

    @Override
    public int beanLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {


        fragHomeBanner = (Banner) findViewById(R.id.frag_home_banner);
        fragHomeEdit = (EditText) findViewById(R.id.frag_home_edit);
        fragHomeTextbanner = (TextBannerView) findViewById(R.id.frag_home_textbanner);
        fragHomeRv = (RecyclerView) findViewById(R.id.frag_home_rv);
        layoutVpBanner = (BannerViewPager) findViewById(R.id.layout_vp_banner);
        mHomeGoodsList = (RecyclerView) findViewById(R.id.mHomeGoodsList);
        mHomeGoodsList.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        tvGoodstypeLive = (DrawableCenterTextView) findViewById(R.id.tv_goodstype_live);
    }

    @Override
    public void initData() {

        //搜索图标
        sousuo();

        //轮播图
        banner();

        //广告垂直轮播
        advertise();

        //折扣
        discount();

        //下轮播
        vpbanner();

        //商品详情
        goods();

        //点击搜索商品
        showGoods();

        //点击进入直播
        golive();

    }

    private void golive() {
        tvGoodstypeLive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LiveActivity.class);
                startActivity(intent);
            }
        });
    }


    private void showGoods() {
        fragHomeEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(RouterPath.SearchGoods.SEARCH_GOODS)
                        .navigation();

            }
        });
    }

    private void goods() {

        mPresenter = new GoodsPresenter(new GoodsModel(), this);
        mPresenter.getGoods("男", "男装", 1, 20);

        Log.i("111111111111TAG", "goods: " + mPresenter);

        //ScrollView嵌套RecyclerView滑动冲突解决----管理器
        ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(getContext());
        //不启动
        scrollLinearLayoutManager.setScrollEnable(false);
    }

    private void vpbanner() {

        ArrayList<String> strings = new ArrayList<>();
        strings.add(MainConstant.Banner_one);
        strings.add(MainConstant.Banner_two);
        strings.add(MainConstant.Banner_three);
        strings.add(MainConstant.Banner_four);

        VpBannerAdapter vpBannerAdapter = new VpBannerAdapter();

        layoutVpBanner.setPageStyle(PageStyle.MULTI_PAGE_SCALE)
                .setRevealWidth(80)
                .setLifecycleRegistry(getLifecycle())
                .setIndicatorVisibility(View.INVISIBLE)
                .setAdapter(vpBannerAdapter)
                .create(strings);
    }

    private void discount() {

        ArrayList<String> strings = new ArrayList<>();
        strings.add(MainConstant.Phone_one);
        strings.add(MainConstant.Phone_two);
        strings.add(MainConstant.Phone_three);
        strings.add(MainConstant.Phone_four);
        strings.add(MainConstant.Phone_five);
        strings.add(MainConstant.Phone_six);
        strings.add(MainConstant.Phone_seven);


        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        fragHomeRv.setLayoutManager(manager);
        DiscountAdapter discountAdapter = new DiscountAdapter(strings);
        fragHomeRv.setAdapter(discountAdapter);
    }

    private void advertise() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("夏日炎炎，第一波福利还有30秒到达战场,新用户立领1000元优惠券");
        strings.add("新用户立领1000元优惠券");
        fragHomeTextbanner.setDatas(strings);
    }

    private void banner() {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(R.drawable.banner_one);
        integers.add(R.drawable.home_banner_two);
        integers.add(R.drawable.home_banner_three);

        fragHomeBanner.setImages(integers);
        fragHomeBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                imageView.setImageResource((Integer) path);
            }
        });

        fragHomeBanner.setBannerAnimation(Transformer.Accordion);
        fragHomeBanner.setDelayTime(2000);
        //设置指示器位置（当banner模式中有指示器时）
        fragHomeBanner.setIndicatorGravity(BannerConfig.RIGHT);
        fragHomeBanner.start();
    }

    private void sousuo() {
        //调搜索框里图片大小的
        Drawable leftDrawable = fragHomeEdit.getCompoundDrawables()[0];
        if (leftDrawable != null) {
            leftDrawable.setBounds(0, 0, 40, 40);
            fragHomeEdit.setCompoundDrawables(leftDrawable, fragHomeEdit.getCompoundDrawables()[1], fragHomeEdit.getCompoundDrawables()[2], fragHomeEdit.getCompoundDrawables()[3]);
        }
    }

    @Override
    public void initAdapter(List<Goods.DataBean> dataBeans) {

        Log.i("initada", "onNext: " + dataBeans);
        mHomeGoodsList.setAdapter(new GoodAdapter(R.layout.item_goods, dataBeans));
    }


}